package com.wiki.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wiki.entity.Category;
import com.wiki.mapper.CategoryMapper;
import com.wiki.service.CategoryService;
import com.wiki.utils.CopyUtil;
import com.wiki.utils.SnowFlake;
import com.wiki.utils.commonRequest.category.CategoryQueryReq;
import com.wiki.utils.commonRequest.category.CategorySaveReq;
import com.wiki.utils.commonResponse.CategoryQueryResp;
import com.wiki.utils.commonResponse.PageResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 分类 服务实现类
 * </p>
 *
 * @author 偷闰土的猹
 * @since 2022-01-07
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SnowFlake snowFlake;

    @Override
    public PageResp<CategoryQueryResp> listAll(CategoryQueryReq req) {

        PageResp<CategoryQueryResp> respList = new PageResp<>();
        if (req.getPage() != 0 && req.getSize() != 0) {
            PageHelper.startPage(req.getPage(), req.getSize());
        }

        List<Category> categorys = list();
        PageInfo<Category> pageInfo = new PageInfo<>(categorys);
        respList.setTotal(pageInfo.getTotal());
        List<CategoryQueryResp> respList1 = CopyUtil.copyList(categorys, CategoryQueryResp.class);
        respList.setList(respList1);

        return respList;
    }

    @Override
    public void save(CategorySaveReq req) {
        Category copy = CopyUtil.copy(req, Category.class);
        if (ObjectUtils.isEmpty(req.getId())) {
            //新增
            Long id = snowFlake.nextId();
            copy.setId(id);
            categoryMapper.insert(copy);
        } else {
            //更新
            categoryMapper.updateById(copy);
        }
    }

    @Override
    public void delete(Long id) {
        int i = categoryMapper.deleteById(id);
        if (i > 0)
            System.out.println("删除了：" + id);
        else
            System.out.println("删除失败！id为：" + id);
    }

    @Override
    public PageResp<CategoryQueryResp> queryLike(CategoryQueryReq req) {
        PageResp<CategoryQueryResp> respList = new PageResp<>();
        PageHelper.startPage(req.getPage(), req.getSize());
//        QueryWrapper<Category> wrapper = new QueryWrapper<Category>().like("name",req.getName());
        List<Category> categorys = categoryMapper.selectList(null);
        PageInfo<Category> pageInfo = new PageInfo<>(categorys);
        respList.setTotal(pageInfo.getTotal());
        List<CategoryQueryResp> respList1 = CopyUtil.copyList(categorys, CategoryQueryResp.class);
        respList.setList(respList1);

        return respList;
    }

    @Override
    public List<CategoryQueryResp> all() {
        QueryWrapper<Category> wrapper = new QueryWrapper<Category>().orderByAsc("sort");
        List<Category> categories = categoryMapper.selectList(wrapper);

        List<CategoryQueryResp> categoryQueryResps = CopyUtil.copyList(categories, CategoryQueryResp.class);
        return categoryQueryResps;
    }


}
